# -*- coding: utf-8 -*-
from sqlalchemy import Column, Integer, BigInteger, String, CHAR, DateTime
from datasource import Base
from datetime import datetime
from common.DateUtil import getStdTimeStr

class User(Base):
    __tablename__ = 'user'
    id = Column(BigInteger, primary_key=True)
    email = Column(String(32), nullable=True)
    phone = Column(String(16), nullable=True)
    password = Column(String(16), nullable=False)
    gmt_create = Column(DateTime, nullable=False)
    gmt_modified = Column(DateTime, nullable=False)

    def __init__(self, email, phone, password, gmt_create, gmt_modified):
        self.email = email
        self.phone = phone
        self.password = password
        self.gmt_create = gmt_create
        self.gmt_modified = gmt_modified

    def check_password(self, password):
        return self.password == password

    def __repr__(self):
        return '<User %r>' % self.email

    def entity(self):
        return {'id':self.id, 'email' : self.email, 'phone' : self.phone, 'password' : self.password, 'gmt_create' : getStdTimeStr(self.gmt_create), 'gmt_modified' : getStdTimeStr(self.gmt_modified)}

class UserInfo(Base):
    __tablename__ = 'user_info'
    uid = Column(BigInteger, primary_key=True)
    name = Column(String(16), nullable=False)
    sex = Column(CHAR(1), nullable=True)
    city = Column(String(16), nullable=True)
    brief = Column(String(128), nullable=True)
    avatar = Column(String(256), nullable=True)
    gmt_create = Column(DateTime, nullable=False)
    gmt_modified = Column(DateTime, nullable=False)

    def __init__(self, uid, name, sex, city, brief, avatar):
        self.uid = uid
        self.name = name
        self.sex = sex
        self.city = city
        self.brief = brief
        self.avatar = avatar
        self.gmt_create = datetime.now()
        self.gmt_modified = datetime.now()

    def __repr__(self):
        return '<User %r>' % self.name

    def entity(self):
        return {'uid':self.uid, 'name' : self.name, 'sex' : self.sex, 'city' : self.city, 'brief':self.brief, 'avatar':self.avatar, 'gmt_create' : getStdTimeStr(self.gmt_create), 'gmt_modified' : getStdTimeStr(self.gmt_modified)}

